﻿using M.Warehouse.API.Application.Command.InventoryCommand;
using M.Warehouse.Domain;
using M.Warehouse.Domain.DTO;
using M_ApiResult;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace M.Warehouse.API.Controllers
{
    /// <summary>
    /// 库存控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InventoryController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="mediator"></param>
        public InventoryController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加库存
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Create(InventoryCreateCommand command)
        {
            ApiResult<int> result=new ApiResult<int>();
            try
            {
                result.Data=await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除库存
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Delete([FromQuery]InventoryDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改库存状态
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdState([FromQuery] InventoryUpdStateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改库存
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Update(InventoryUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示库存(入库)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Inventory>>> Show([FromQuery]InventoryShowCommand command)
        {
            ApiResult<List<Inventory>> result = new ApiResult<List<Inventory>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示库存(出库)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Inventory>>> OutShow([FromQuery] OutInventoryShowCommand command)
        {
            ApiResult<List<Inventory>> result = new ApiResult<List<Inventory>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示库存(转库)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Inventory>>> TransferShow([FromQuery] TransferShowCommand command)
        {
            ApiResult<List<Inventory>> result = new ApiResult<List<Inventory>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 返填库存
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Inventory>> Backfill([FromQuery] InventoryBackfillCommand command)
        {
            ApiResult<Inventory> result = new ApiResult<Inventory>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 根据产品编号显示入库信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<InListDto>>> ShowGetCode([FromQuery] InventoryGetCodeCommand command)
        {
            ApiResult<List<InListDto>> result = new ApiResult<List<InListDto>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
